package project.Service;/*
 *  @Author Mr.long
 *  @QQ 1507468405
 *  @Creat 2021/11/24 - 23:17
 */

import org.json.JSONException;
import org.json.JSONObject;
import project.Dao.MeetDao;
import project.Dao.UserDao;
import project.Domain.Meeting;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;

@WebServlet(name = "MeetServlet", value = "/MeetServlet")
public class MeetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.service(req, resp);
        /*
                    前端传入当前登录用户的id,以及对应的通知消息json对象
                 */
        //创建Dao对象来操作数据库
        MeetDao meetDao = new MeetDao();
        //操作方式
        String action = req.getParameter("action");
        //用户id
        String user = req.getParameter("id");
        //用户角色
        String role = req.getParameter("role");

        //通知主体
        String meet_id = req.getParameter("meet");
        //通知标题
        String title = req.getParameter("title");
        //通知主体
        String content = req.getParameter("content");
        //通知发布时间
        String time = req.getParameter("deliverTime");
        String meetingTime = req.getParameter("meetingTime");
        Integer id = Integer.parseInt(user);
        //封装返回数据
        JSONObject ret = new JSONObject();
        /* 操作分流*/
        switch (action){
            case "query":
                ret =  getAllMeeting(role,id);
                out.println(ret);
                break;
            case "add":
                //新增一条通知
                UserDao add = new UserDao();
                Meeting meet = null;
                if("组员".equals(role)){
                    //组员，将发布者编号设为组长编号
                    meet = new Meeting(-1, add.getLeaderID(id), add.getNameById(id), title, content, time,meetingTime);
                }else{
                    //组长或者管理员，将发布者编号设为自己的编号
                    meet = new Meeting(-1,id,add.getNameById(id),title,content, time,meetingTime);
                }

                try {
                    if(meetDao.insert(meet)){
                        ret.put("msg", "ok");
                    }else{
                        ret.put("msg", "error");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //邮件发送通知
                break;
            case "delete":
                //删除一条通知
                try {
                    if(meetDao.delete(Integer.parseInt(meet_id))){
                        ret.put("msg", "ok");
                    }else{
                        ret.put("msg", "error");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case "edit":
                //修改一条通知
                UserDao update = new UserDao();
                Meeting edit = null;
                if("组员".equals(role)){
                    edit = new Meeting(Integer.parseInt(meet_id), update.getLeaderID(id), update.getNameById(id), title,
                            content, time,meetingTime);
                }else{
                    edit = new Meeting(Integer.parseInt(meet_id), id, update.getNameById(id), title, content, time,meetingTime);
                }
                try {
                    if(meetDao.update(edit)){
                        ret.put("msg", "ok");
                    }else{
                        ret.put("msg", "error");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
        resp.setContentType("text/html; charset=UTF-8");
//        out.println(ret);
        try {
            resp.getWriter().print(ret);
            resp.getWriter().flush();
            resp.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public JSONObject getAllMeeting(String role,int id){
        JSONObject ret = new JSONObject();
        //接收查询结果
        List<Meeting> meetings = null;
        MeetDao meetDao = new MeetDao();
        switch (role){
            case "组员":
                //查询组内通知
                meetings = meetDao.queryAll(new UserDao().getLeaderID(id));
                break;
            case "组长":
                //查询管理员发布的通知
                meetings = meetDao.queryAll(new UserDao().getLeaderID(id));
                //查询组内通知
                meetings.addAll(meetDao.queryAll(id));
                break;
            case "管理员":
                meetings= meetDao.queryAll(id);
                break;
        }
        List retList = new ArrayList<>();

        for (Meeting meet : meetings) {
            Map map = new HashMap<>();
            map.put("id", meet.getId());
            map.put("publisherName", meet.getPublisherName());
            map.put("title", meet.getTitle());
            map.put("content", meet.getContent());
            map.put("deliverTime", meet.getDeliverTime().split("\\.")[0]);
            map.put("meetTime", meet.getMeetingTime().split("\\.")[0]);
            retList.add(map);
        }
//        out.println(retList);
        try {
            ret.put("meetings", retList);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
